// pages/usercenter/pladge/pladge.js
import {
  users
} from '../../../../tools/users'
import {
  ui
} from '../../../../tools/ui'
import {
  pay
} from '../../../../tools/pay'
import {
  checkTool
} from '../../../../tools/check'
import {
  msg
} from '../../../../tools/msg'
import {
  ds
} from '../../../../tools/data'
import {
  dsx
} from '../../../../tools/datax'
import {
  dateTool
} from '../../../../tools/date';
import {
  nav2
} from '../../../../tools/nav2'

Page({
  data: {
    bindUsers: null,
    user: null,
    uopenid: null
  },
  onLoad(options) {
    this.setData({
      uopenid: options.uopenid
    })
    this.init()
  },
  init() {
    ui.loading()
    this.loadMyInfo()
  },
  async loadMyInfo() {
    users.getUserInfo()
      .then(user => {
        const uopenid = this.data.uopenid || user._openid
        this.setData({
          user,
          uopenid,
        })
        this.loadBindUsers({
          _openid: uopenid
        })
      })
  },
  async loadBindUsers(user) {
    const _ = dsx.cmd
    dsx.getAll('users', {
      bind: _.eq(user._openid)
    }).then(res => {
      this.setData({
        ...this.data,
        bindUsers: res
      })
      ui.loading(false)
    })
  },
  callMobile(e) {
    const mobile = ui.eventData(e, "mobile")
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  nickNameTap(e) {
    if (this.data.user.role != 'admin') return

    const uid = ui.eventData(e, 'uid')
    nav2.staffEdit('edit', uid)
  },
  myAdvCodeTap() {
    wx.navigateTo({
      url: '/pages/qr/qrcode?param=' + JSON.stringify({
        title: '推广'
      }),
    })
  },
  deductFeeTap() {
    users.getUserByOpenid(this.data.uopenid, {
        _id: true,
        personInfo: {
          nickName: true
        }
      })
      .then(user => {
        wx.navigateTo({
          url: `/pages/usercenter/staff/deductFee/deductFee?staffId=${user._id}&nickName=${user.personInfo.nickName}`,
        })
      })
  },
  onPullDownRefresh() {
    this.init(this.data.action)
  }
})